package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R<Page> bypage(int page, int pageSize, String name) {
        //1:创建分页构造器
        Page<Setmeal> pageinfo = new Page<>(page,pageSize);
        //2:创建条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //3:设置排序字段
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //4:分页查询
        this.page(pageinfo,queryWrapper);
        //对象拷贝
        Page<SetmealDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageinfo,dtoPage,"records");
        //完成内容的拷贝
        List<Setmeal> records = pageinfo.getRecords();
        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            //根据categoryid，查找分类id
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            list.add(setmealDto);
        }
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    /**
     * 添加套餐，同时添加套餐与菜品的关系数据
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public R<String> saveWithSetmeadish(SetmealDto dto) {
        //1：添加套餐
        this.save(dto);
        //2:添加数据到setmeal_dish中去
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            dish.setSetmealId(dto.getId());
        }
        //3:添加数据到套餐与菜品关系表中
        setmealDishService.saveBatch(setmealDishes);
        return R.success("新增成功");

    }

    /**
     * 根据页面提交的信息修改套餐信息和套餐菜品关联表中的信息
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public R<String> update(SetmealDto dto) {
        //1：先修改套餐表中的数据(根据category进行修改)
        //2：修改数据
        this.lambdaUpdate().eq(Setmeal::getCategoryId, dto.getCategoryId()).update(dto);
        //3:对categorydish中数据进行修改(先删除之前的数据)
        //3.1:创建一个构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //3.2:设置条件
        queryWrapper.eq(SetmealDish::getSetmealId,dto.getId());
        //3.3删除记录
        setmealDishService.remove(queryWrapper);
        //3.4:添加新修改的套餐与菜品的数据记录
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            dish.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }

    /**
     * 删除套餐信息同时删除套餐与菜品的关联信息  同时也可实现批量删除
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public R<String> deletewithdish(List<Long> ids) {


        //1：先要判断是否为停售状态,因为只有再停售状态套餐才能被删除
        for (Long id : ids) {
            Setmeal setmeal = this.getById(id);
            if(setmeal.getStatus()==1){
                //如果不能删除,直接抛一个异常,之前自定义的那个异常可以用
                throw new CustomException("当前套餐正在售卖,请先停售再来删除");
            }
        }
        //2:删除套餐信息
        this.removeByIds(ids);
        //3:删除套餐菜品关联表中对应的数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper);
        return R.success("删除成功");
    }
}
